package controller

import (
	"hft/Database/admin"
	"hft/Http/Models/request"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Api struct{}

func (*Api) GetApiList(c *gin.Context) {

	var paginate utils.Page

	c.ShouldBind(&paginate)

	id, _ := strconv.Atoi(c.DefaultQuery("id", "0"))

	if id == 0 {
		utils.FailWithMessage("不存在该分组", c)
		return
	}

	list, err := services.ApiService.GetApis(id, &paginate)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(list, c)

}

func (*Api) Create(c *gin.Context) {

	var data *admin.Api

	c.ShouldBindJSON(&data)

	err := services.ApiService.Create(data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*Api) Edit(c *gin.Context) {

	var data *admin.Api

	c.ShouldBindJSON(&data)

	err := services.ApiService.Edit(data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*Api) Delete(c *gin.Context) {

	var ids *request.IDS

	c.ShouldBindJSON(&ids)

	err := services.ApiService.Delete(ids.IDS)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

var ApiController = new(Api)
